package com.green.web;

import com.green.domain.Message;
import com.green.domain.User;
import com.green.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by WHUER on 2016/6/12 0012.
 */
@Controller
public class MessageController extends BaseController {
    @Autowired
    private MessageService messageService;

    @ResponseBody
    @RequestMapping(value = "/messageNum",method = RequestMethod.GET)
    public long getMessageCount(HttpServletRequest request)
    {
        User user= this.getSessionUser(request);
        return messageService.getMessageCount(user);
    }

    @RequestMapping(value = "/myMessage",method = RequestMethod.GET)
    public ModelAndView getMyMessage(HttpServletRequest request)
    {
        ModelAndView mav =new ModelAndView();
        List<Message> messageList=messageService.getMyMessage(getSessionUser(request));
        for(Message message: messageList)
        {
            System.out.println("牛中超在测试输出:"+message.toString());
        }
        mav.addObject("msgList",messageList);

        mav.setViewName("forward:/msgList.jsp");
        return mav;
    }

    @RequestMapping(value = "/msgDetail",method = RequestMethod.GET)
    public ModelAndView getOneMsgDetail(HttpServletRequest request,int id)
    {
        ModelAndView mav =new ModelAndView();
        Message msg = messageService.getOneMessageDetail(id);
        mav.setViewName("forward:/msgDetail.jsp");
        mav.addObject("msgDetail",msg);
        //这里将当前查看的消息设置为已读
        msg.setStatus(0);
        messageService.update(msg);
        return mav;
    }

    @RequestMapping(value = "/sendMessage",method =RequestMethod.POST)
    public ModelAndView sendMessage(@ModelAttribute  Message message, BindingResult result)
    {
        System.out.println(message.toString());
        ModelAndView mav= new ModelAndView("forward:/sendStatus.jsp");
        if(result.hasErrors()) {
            List<FieldError> errors = result.getFieldErrors();
            for(FieldError err : errors) {
                System.out.println("ObjectName:" + err.getObjectName() + "\tFieldName:" + err.getField()
                        + "\tFieldValue:" + err.getRejectedValue() + "\tMessage:" + err.getDefaultMessage());
            }
            mav.addObject("message", "发送失败");
            return mav;
        }
        message.setSendTime(new Date());
        message.setStatus(1);
        messageService.save(message);
        mav.addObject("message","发送成功");
        mav.addObject("url","index.jsp");
        return mav;
    }
}

